//
//  DataConfirmationAnimationInfoView.swift
//  Yolan
//
//  Created by Qiu Jishuai on 2018/4/7.
//  Copyright © 2018年 jsh. All rights reserved.
//

import UIKit
import pop

private let width: CGFloat = 325.0
private let height: CGFloat = 400.0

class DataConfirmationAnimationInfoView: UIView {

    @IBOutlet weak var monitorTopView: UIView! /// 检测中一栏 白底View
    @IBOutlet weak var outterImageView: UIImageView!  /// 最外层圈
    @IBOutlet weak var lineImageView: UIImageView!  /// 线框
    @IBOutlet weak var anchorImageView: UIImageView!  /// 锚点
    @IBOutlet weak var valueStackView: UIStackView!
    @IBOutlet weak var valueImageView: UILabel!     /// 值
    @IBOutlet weak var lineMaskView: UIView!      /// lineImageView的父视图
    @IBOutlet weak var statusMaskView: UIView!      /// 检测中三个点的遮罩view
    
    var leftAnchorImgView_X: CGFloat = 0.0   // 左侧 锚点左移的距离
    var upOrDownAnchorImgView_Y: CGFloat = 0.0 // 锚点上下移动的距离
    
    var positionInfo: Position! {
        didSet {
            initPositionInfo()
        }
    
    }
    // 接收的点
    var pointCoord: CGPoint!
    var viewX : CGFloat!
    var viewY : CGFloat!
    var linemaskRect: CGRect!

    private func initPositionInfo() {
        
        switch positionInfo {
        case .leftTop, .leftBottom:
            /// 左下 左下
            lineMaskView.anchorPoint = CGPoint(x: 0, y: 0.5)
            
        default:
            /// 右下  右上
            lineMaskView.anchorPoint = CGPoint(x: 1, y: 0.5)
        }
        
    }
    

    override func awakeFromNib() {
        super.awakeFromNib()
        upOrDownAnchorImgView_Y = lineImageView.frame.size.height+5
        leftAnchorImgView_X = self.bounds.width + anchorImageView.frame.size.width/2
        
        valueStackView.isHidden = true
        anchorImageView.isHidden = true
        
        outterImageView.addBasicAnimation(with: .scaleXY,
                                          duration: 0,
                                          toValue: CGPoint(x: 0, y: 0),
                                          repeatForever: false,
                                          beginTime: 0)
        monitorTopView.addBasicAnimation(with: .scaleX,
                                         duration: 0,
                                         toValue: 0,
                                         repeatForever: false,
                                         beginTime: 0)
        lineMaskView.clipsToBounds = true
    }
    
    func setupInitAnimationInfoViewPoint(at point:CGPoint, position: Position) {
        pointCoord = point
        
        viewX = (position == .leftTop || position == .leftBottom) ? pointCoord.x + 41 : pointCoord.x+41-width-90
        viewY = pointCoord.y-height/2+80
        
        self.frame = CGRect(x: viewX, y: viewY, width: width, height: height)
        positionInfo = position

        lineMaskView.addBasicAnimation(with: .scaleX, duration: 0, toValue: 0,repeatForever: false, beginTime: 0)

        
        switch position {
        case .leftTop:
            /// 左上
                let lineViewWidth = lineImageView.frame.size.width
                lineImageView.image = UIImage(cgImage: (lineImageView.image?.cgImage)!, scale: 1, orientation: UIImageOrientation(rawValue: Int(Double.pi/2))!)

                lineMaskView.addBasicAnimation(with: .translationX,
                                                duration: 0,
                                                toValue: -lineViewWidth/2,
                                                repeatForever: false,
                                                beginTime: 0)
                anchorImageView.addBasicAnimation(with: .translationX,
                                                  duration: 0,
                                                  toValue: -leftAnchorImgView_X,
                                                  repeatForever: false,
                                                  beginTime: 0)
                               .addBasicAnimation(with: .translationY,
                                                  duration: 0,
                                                  toValue: -upOrDownAnchorImgView_Y,
                                                  repeatForever: false,
                                                  beginTime: 0)
                startAnimation()
        
        
        case .rightTop:
            /// 右上
         
            //垂直翻转图片
            var flipImageOrientation = ((lineImageView.image?.imageOrientation.rawValue)! + 4) % 8
            flipImageOrientation += flipImageOrientation%2==0 ? 1 : -1
            lineImageView.image = UIImage(cgImage: (lineImageView.image?.cgImage)!, scale: 1, orientation: UIImageOrientation(rawValue: flipImageOrientation)!)
            anchorImageView.addBasicAnimation(with: .translationY,
                                              duration: 0,
                                              toValue: -upOrDownAnchorImgView_Y,
                                              repeatForever: false,
                                              beginTime: 0)
            startAnimation()
            
        
            break
        case .leftBottom:
            /// 左下 水平翻转图片
            let lineViewWidth = lineImageView.frame.size.width
            let flipImageOrientation = ((lineImageView.image?.imageOrientation.rawValue)! + 4) % 8
            lineImageView.image = UIImage(cgImage: (lineImageView.image?.cgImage)!, scale: 1, orientation: UIImageOrientation(rawValue: flipImageOrientation)!)

            lineMaskView.addBasicAnimation(with: .translationX,
                                           duration: 0,
                                           toValue: -lineViewWidth/2,
                                           repeatForever: false,
                                           beginTime: 0)
            anchorImageView.addBasicAnimation(with: .translationX,
                                              duration: 0,
                                              toValue: -leftAnchorImgView_X,
                                              repeatForever: false,
                                              beginTime: 0)
            startAnimation()
        
        case .rightBottom:
            /// 右下
            startAnimation()
        
        }

    }

    // 开始动画
    func startAnimation() {
        anchorImageView.isHidden = false
        //锚点动画
        let anchorDuration: TimeInterval = 0.5
        anchorImageView
            .addBasicAnimation(with: .scaleXY,
                                 duration: 0.1,
                                 fromValue: CGPoint(x: 0.1, y: 0.1),
                                 toValue: CGPoint(x: 1, y: 1),
                                 repeatForever: false,
                                 completionBlock: { (_, _) in
                                    self.lineMaskView!.isHidden = false
                                    
            })
            .addBasicAnimation(with: .rotation,
                                 duration: anchorDuration+0.5,
                                 toValue: -Double.pi * 2)

        //线动画
        let lineDuration: TimeInterval = 0.2
        switch positionInfo {
        case .rightTop, .rightBottom:
            // 右上
            lineMaskView.addBasicAnimation(with: .scaleX,
                                           duration: 0.2,
                                           fromValue: 0,
                                           toValue: 1,
                                           repeatForever: false,
                                           beginTime: 0.2,
                                           completionBlock: { (_, _) in
                                            self.valueStackView!.isHidden = false

            })
            
        case .leftTop, .leftBottom:
            /// 左下, 左上
            lineMaskView.addBasicAnimation(with: .translationX,
                                           duration: 0,
                                           toValue: -lineMaskView.frame.size.width/2,
                                           repeatForever: false,
                                           beginTime: 0)
                .addBasicAnimation(with: .scaleX,
                                   duration: 0.2,
                                   fromValue: 0,
                                   toValue: 1,
                                   repeatForever: false,
                                   beginTime: 0.2,
                                   completionBlock: { (_, _) in
                                    self.valueStackView!.isHidden = false

                })
        default:
            break
        }


        //中心文字
        startValuesAnimation()
        //外侧圆圈
        let outterBegin = lineDuration
        outterImageView
            .addBasicAnimation(with: .scaleXY,
                               duration: 0.3,
                               fromValue: CGPoint(x: 0.5, y: 0.5),
                               toValue: CGPoint(x: 1, y: 1),
                               repeatForever: false,
                               beginTime: outterBegin+0.3)

            .addBasicAnimation(with: .rotation,
                                            duration: 6,
                                            toValue: CGFloat.pi * 2,
                                            beginTime: outterBegin+0.3)

        statusMaskView.addBasicAnimation(with: .scaleX,
                                        duration: 1,
                                        fromValue: 1,
                                        toValue: 0)

        /// 顶部检测一栏
        monitorTopView.addBasicAnimation(with: .scaleX,
                                         duration: 0.3,
                                         fromValue: 0.0,
                                         toValue: 1.0,
                                         repeatForever: false,
                                         beginTime: outterBegin+0.3)
    }

    /// 中心文字
    private func startValuesAnimation() {
        
        valueStackView.addBasicAnimation(with: .scaleXY,
                                         duration: 0.2,
                                         fromValue: CGPoint(x: 0.5, y: 0.5),
                                         toValue: CGPoint(x: 2, y: 2),
                                         repeatForever: false,
                                         beginTime: 0.5)
            .addBasicAnimation(with: .scaleXY,
                                           duration: 0.5,
                                           fromValue: CGPoint(x: 2, y: 2),
                                           toValue: CGPoint(x: 0.5, y: 0.5),
                                           repeatForever: false,
                                           beginTime: 0.7)
            .addBasicAnimation(with: .scaleXY,
                                 duration: 1,
                                 toValue: CGPoint(x: 1, y: 1),
                                 repeatForever: false,
                                 beginTime: 1)
    }
    
    /// 隐藏动画
    func hideAnimation() {

        //外侧圆圈隐藏
        let outterBegin = 0.0
        outterImageView
            .addBasicAnimation(with: .scaleXY,
                               duration: 0.3,
                               fromValue: CGPoint(x: 1, y: 1),
                               toValue: CGPoint(x: 0.0, y: 0.0),
                               repeatForever: false,
                               beginTime: outterBegin)
        
        /// 中心文字
        valueStackView.addBasicAnimation(with: .scaleXY,
                                         duration: 0.2,
                                         fromValue: CGPoint(x: 1, y: 1),
                                         toValue: CGPoint(x: 0, y: 0),
                                         repeatForever: false,
                                         beginTime: 0)

        //线动画
        let lineDuration: TimeInterval = 0.2
        switch positionInfo {
        case .rightTop, .rightBottom:
            // 右上
            lineMaskView.addBasicAnimation(with: .scaleX,
                                           duration: lineDuration,
                                           fromValue: 1,
                                           toValue: 0,
                                           repeatForever: false,
                                           beginTime: 0.1)
        case .leftTop, .leftBottom:
            /// 左下, 左上
            lineMaskView.addBasicAnimation(with: .scaleX,
                                   duration: lineDuration,
                                   fromValue: 1,
                                   toValue: 0,
                                   repeatForever: false,
                                   beginTime: 0.1)
        default:
            break
        }
        
        /// 顶部检测一栏 duration:所有的动画时间之和
        monitorTopView.addBasicAnimation(with: .scaleX,
                                         duration: 0.8,
                                         fromValue: 1.0,
                                         toValue: 0,
                                         repeatForever: false,
                                         beginTime: outterBegin)
        
        /// 锚点
        anchorImageView
            .addBasicAnimation(with: .scaleXY,
                               duration: 0.1,
                               fromValue: CGPoint(x: 1, y: 1),
                               toValue: CGPoint(x: 0.0, y: 0.0),
                               repeatForever: false,
                               beginTime: lineDuration+0.2,
                               completionBlock: { (_, _) in
                                
                                self.removeFromSuperview()
            })

    }

    override func layoutSubviews() {
        super.layoutSubviews()
        
            monitorTopView.anchorPoint = CGPoint(x: 0, y: 0)
    }
}


//extension UIView {
//    
//    enum Position {
//        case leftTop
//        case leftBottom
//        case rightTop
//        case rightBottom
//        
//    }
//
//    enum PopAnimationType {
//        case scaleX
//        case scaleY
//        case scaleXY
//        
//        case rotation
//        case rotationX
//        case rotationY
//
//        case translationX
//        case translationXY
//        case translationY
//        case translationZ
//
//        var property: String {
//            switch self {
//            case .scaleX: return kPOPViewScaleX
//            case .scaleY: return kPOPViewScaleY
//            case .scaleXY: return kPOPViewScaleXY
//            case .rotation: return kPOPLayerRotation
//            case .rotationX: return kPOPLayerRotationX
//            case .rotationY: return kPOPLayerRotationY
//            case .translationX: return kPOPLayerTranslationX
//            case .translationY: return kPOPLayerTranslationY
//            case .translationXY: return kPOPLayerTranslationXY
//            case .translationZ: return kPOPLayerTranslationZ
//            }
//        }
//    }
//
//    enum PopTimingFunctionType {
//        case linear
//        case easeIn
//        case easeInEaseOut
//        case easeOut
//        case custom(String)
//        var function: String {
//            switch self { //kCAAnimationLinear
//            case .linear:
//                return kCAMediaTimingFunctionLinear   // 匀速
//            case .easeInEaseOut:
//                return kCAMediaTimingFunctionEaseInEaseOut // 先慢后快再慢
//            case .easeIn:
//                return kCAMediaTimingFunctionEaseIn  //  先慢后快
//            case .easeOut:
//                return kCAMediaTimingFunctionEaseOut //  先快后慢
//            case .custom(let function):
//                return function
//            }
//        }
//    }
//
//    @discardableResult
//    func addBasicAnimation(with type: PopAnimationType,
//                        duration: TimeInterval = 1,
//                        fromValue: Any? = nil,
//                        toValue: Any,
//                        timingFunction: PopTimingFunctionType = .linear,
//                        repeatCount: Int = 0,
//                        repeatForever: Bool = true,
//                        beginTime: CFTimeInterval = 0,
//                        autoreverses: Bool = false,
//                        key: String? = nil,
//                        advanceBlock: ((UIView) -> Void)? = nil,
//                        completionBlock: ((POPBasicAnimation, Bool) -> Void)? = nil) -> Self {
//        advanceBlock?(self)
//        let animation = POPBasicAnimation(propertyNamed: type.property)!
//        animation.duration = duration
//        animation.repeatForever = repeatForever
//        animation.fromValue = fromValue
//        animation.toValue = toValue
//        animation.repeatCount = repeatCount
//        animation.autoreverses = autoreverses
//        animation.timingFunction = CAMediaTimingFunction(name:timingFunction.function)
//        animation.beginTime = beginTime == 0 ? 0 : CACurrentMediaTime() + beginTime
//        if let completionBlock = completionBlock {
//            animation.completionBlock = { (anim, finish) in
//                completionBlock(anim as! POPBasicAnimation, finish)
//            }
//        }
//
//        switch type {
//        case .scaleX, .scaleY, .scaleXY:
//            pop_add(animation, forKey: key)
//        default: layer.pop_add(animation, forKey: key)
//        }
//        return self
//    }
//}
